using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_CommonItemTips : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_CommonItemTips
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExArtSpriteNum bgQuality { protected set; get; }
            public IQIGame.Onigao.Framework.ExArtSpriteNum nameBg { protected set; get; }
            public IQIGame.Onigao.Framework.ExText nameText { protected set; get; }
            public IQIGame.Onigao.Framework.ExText typeText { protected set; get; }
            public IQIGame.Onigao.Framework.ExText numText { protected set; get; }
            public UnityEngine.GameObject commonItem { protected set; get; }
            public UnityEngine.GameObject useEffectRoot { protected set; get; }
            public IQIGame.Onigao.Framework.ExText effectDes { protected set; get; }
            public UnityEngine.GameObject sourceRoot { protected set; get; }
            public UnityEngine.GameObject sourceMould { protected set; get; }
            public IQIGame.Onigao.Framework.ExText desText { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton buttonUse { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("bgQuality", out var __tbv0);
                this.bgQuality = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExArtSpriteNum>("nameBg", out var __tbv1);
                this.nameBg = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("nameText", out var __tbv2);
                this.nameText = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("typeText", out var __tbv3);
                this.typeText = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("numText", out var __tbv4);
                this.numText = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("commonItem", out var __tbv5);
                this.commonItem = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("useEffectRoot", out var __tbv6);
                this.useEffectRoot = __tbv6;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("effectDes", out var __tbv7);
                this.effectDes = __tbv7;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("sourceRoot", out var __tbv8);
                this.sourceRoot = __tbv8;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("sourceMould", out var __tbv9);
                this.sourceMould = __tbv9;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("desText", out var __tbv10);
                this.desText = __tbv10;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonUse", out var __tbv11);
                this.buttonUse = __tbv11;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private UIN_OneItem item = null;
        private ListScrollAdapter<UIN_SourceFrom> srcFroms = null;

        private ItemData itemData = null;

        protected UIB_UIN_CommonItemTips ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_CommonItemTips();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.item = this.InitChildNode<UIN_OneItem>(this.ui.commonItem);
            this.srcFroms = this.InitListScroll<UIN_SourceFrom>(this.ui.sourceMould, this.OnSoureChanged);

            this.ui.buttonUse.onClick.AddListener(this.OnClickUse);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {

        }

        public void Show(ItemData itemData)
        {
            var cfgData = itemData?.cfgData;
            if (!this.item.Show(cfgData))
            {
                this.Hide();
                return;
            }
            this.Show();
            this.itemData = itemData;

            this.ui.bgQuality.SetInt(cfgData.Quality);
            this.ui.nameBg.SetInt(cfgData.Quality);
            this.ui.nameText.text = cfgData.Name;
            this.ui.desText.text = cfgData.Describe;

            int cnt = ItemModule.Instance.GetNum(itemData.cid);
            this.ui.numText.text = cnt.ToString();
            this.ui.typeText.text = cfgData.TypeDesc;

            bool isCanUse = (cfgData.BatchUseLimit > 0) && (cfgData.ItemEffectID > 0);
            this.ShowUseEffect(isCanUse);
            this.ShowUseEffectBtn(isCanUse);
            this.ShowSource();
        }

        private void ShowUseEffect(bool isCanUse)
        {
            this.ui.useEffectRoot.SetActive(isCanUse);
            if (isCanUse)
            {
                this.ui.effectDes.text = itemData.cfgData.EffectTxt;
            }
        }

        private void ShowUseEffectBtn(bool isCanUse)
        {
            this.ui.buttonUse.SetActive(isCanUse);
        }

        private void ShowSource()
        {
            this.ui.sourceRoot.SetActive(false);
            // this.srcFroms.RefillCells(0);
        }

        private void OnSoureChanged(UIN_SourceFrom itemObj, int index)
        {

        }

        private void OnClickUse()
        {
            if (itemData == null)
                return;

            var cfgEffect = TableCenter.systemEffect.Get(itemData.cfgData.ItemEffectID);
            if (cfgEffect == null)
                return;
            if (IsSelfChoose((EffectType)cfgEffect.Type))
            {
                ShowItemSelfChoose();
            }
            else
            {
                ShowItemUse();
            }
        }

        private bool IsSelfChoose(EffectType effectType)
        {
            return effectType switch
            {
                EffectType.GitChooseItem => true,
                EffectType.GitChooseEquip => true,
                EffectType.GitChooseHero => true,
                _ => false,
            };
        }

        private void ShowItemSelfChoose()
        {
            this.OpenUI<UIChooseGift>(UD_ChooseGift.Create(itemData));
        }

        private void ShowItemUse()
        {
            UD_ItemUse udItemUse = UD_ItemUse.NewFromPool().Sync(APIUIConst.GetItemUseTipTitle(), itemData.id, itemData.cid);
            this.OpenUI<UIItemUse>(udItemUse);
        }
    }
}
